﻿Imports System.Xml.Linq

Partial Public Class Page
    Inherits UserControl

    Private WithEvents WC As New WebClient

    'REPLACE THIS WITH THE LINK TO YOUR PROXY
    'use "http://localhost:50321/" for testing
    Private Const PROXY As String = "http://localhost:50321/"
    'Note: the username must be set on the proxy page (PicasaProxy/Default.aspx.vb)

    Private nsAtom As XNamespace = "http://www.w3.org/2005/Atom"
    Private nsGoogle As XNamespace = "http://schemas.google.com/photos/2007"
    Private nsMrss As XNamespace = "http://search.yahoo.com/mrss/"

    Private Albums As List(Of Album)
    Private Photos As New PhotoPages

    Private PhotoIndex As Integer

    Private AuthorInfo As Author

    Private Enum GetTypes
        Albums
        Photos
    End Enum
    Private ToGet As GetTypes

    Public Sub New()
        InitializeComponent()
        GetAlbumList()
    End Sub

#Region " Downloading the XML "

    Private Sub GetAlbumList()
        WC.DownloadStringAsync(New Uri(PROXY & "?type=album"))
        ToGet = GetTypes.Albums
    End Sub

    Private Sub GetPhotoList(ByVal AlbumName As String)
        If grdLoading.Visibility = Windows.Visibility.Collapsed Then
            cmbAlbums.IsEnabled = False
            grdLoadingPics.Visibility = Windows.Visibility.Visible
            FadeInLoadingPics.Begin()
        End If
        WC.DownloadStringAsync(New Uri(PROXY & "?type=photo&album=" & AlbumName))
        ToGet = GetTypes.Photos
    End Sub

    Private Sub WC_DownloadStringCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs) Handles WC.DownloadStringCompleted
        Select Case ToGet
            Case GetTypes.Albums
                GetAuthorInfo(e.Result)
                Albums = LoadAlbums(e.Result)
                cmbAlbums.ItemsSource = Albums
                cmbAlbums.SelectedIndex = 0 'todo
            Case GetTypes.Photos
                Photos.Photos = LoadPhotos(e.Result)
                lstPhotos.ItemsSource = Photos.PhotoPage
                FillPageList()
                If grdLoading.Visibility = Windows.Visibility.Visible Then fadeoutloading.Begin()
                FadeOutLoadingPics.Begin()
                cmbAlbums.IsEnabled = True
        End Select
    End Sub

#End Region

#Region " Navigating "

    Private Sub cmbAlbums_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cmbAlbums.SelectionChanged
        GetPhotoList(Albums.Item(cmbAlbums.SelectedIndex).Name)
        GridAlbum.DataContext = Albums.Item(cmbAlbums.SelectedIndex)
    End Sub

    Private Sub lstPages_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lstPages.SelectionChanged
        Photos.PageNumber = lstPages.SelectedIndex
        lstPhotos.ItemsSource = Photos.PhotoPage
    End Sub

    Private Sub lnkPicasaAlbum_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles lnkPicasaAlbum.Click
        System.Windows.Browser.HtmlPage.Window.Navigate(New Uri(lnkPicasaAlbum.Content), "_blank")
    End Sub

    Private Sub FillPageList()
        lstPages.Items.Clear()
        For i As Integer = 1 To Photos.NumberOfPages
            lstPages.Items.Add(i.ToString)
        Next
        lstPages.SelectedIndex = 0
    End Sub

    Private Sub lnkProjectPage_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles lnkProjectPage.Click
        System.Windows.Browser.HtmlPage.Window.Navigate(New Uri("http://www.codeplex.com/PicasaLight/"), "_blank")
    End Sub

#End Region

#Region " Fading animations "

    Private Sub fadeoutloading_Completed(ByVal sender As Object, ByVal e As System.EventArgs) Handles fadeoutloading.Completed
        grdLoading.Visibility = Windows.Visibility.Collapsed
    End Sub

    Private Sub FadeOutLoadingPics_Completed(ByVal sender As Object, ByVal e As System.EventArgs) Handles FadeOutLoadingPics.Completed
        grdLoadingPics.Visibility = Windows.Visibility.Collapsed
    End Sub

#End Region

#Region " Big photo viewer "

    Private Sub lstPhotos_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles lstPhotos.SelectionChanged
        If lstPhotos.SelectedIndex >= 0 Then
            PhotoIndex = (Photos.PageNumber * Photos.ItemsPerPage) + (lstPhotos.SelectedIndex + 1) - 1
            grdPhoto.DataContext = Photos.Photos.Item(PhotoIndex)
            EnableDisableNextPrevious()
            grdPhoto.Visibility = Windows.Visibility.Visible
            SpinThatThing.Begin()
        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNext.Click
        PhotoIndex += 1
        grdPhoto.DataContext = Photos.Photos.Item(PhotoIndex)
        EnableDisableNextPrevious()
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnPrevious.Click
        PhotoIndex -= 1
        grdPhoto.DataContext = Photos.Photos.Item(PhotoIndex)
        EnableDisableNextPrevious()
    End Sub

    Private Sub EnableDisableNextPrevious()
        If PhotoIndex > 0 Then
            btnPrevious.IsEnabled = True
        Else
            btnPrevious.IsEnabled = False
        End If
        If PhotoIndex < Photos.Photos.Count - 1 Then
            btnNext.IsEnabled = True
        Else
            btnNext.IsEnabled = False
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        grdPhoto.Visibility = Windows.Visibility.Collapsed
        SpinThatThing.Stop()
    End Sub

#End Region

#Region " LINQ to XML "

    Private Sub GetAuthorInfo(ByVal str As String)
        Dim feedXML As XDocument = XDocument.Parse(str)
        Dim feeds = From feed In feedXML.Descendants(nsAtom + "feed") _
                    Select New Author With { _
                       .AuthorName = feed.Element(nsAtom + "author").Element(nsAtom + "name").Value, _
                       .AuthorPicasaURL = New Uri(feed.Element(nsAtom + "author").Element(nsAtom + "uri").Value), _
                       .AuthorPictureURL = feed.Element(nsGoogle + "thumbnail").Value _
                    }
        AuthorInfo = feeds(0)
    End Sub

    Private Function LoadAlbums(ByVal str As String) As List(Of Album)
        Dim feedXML As XDocument = XDocument.Parse(str)
        Dim feeds = From feed In feedXML.Descendants(nsAtom + "entry") _
                    Select New Album(AuthorInfo.AuthorPicasaURL) With { _
                       .ID = feed.Element(nsGoogle + "id").Value, _
                       .Name = feed.Element(nsGoogle + "name").Value, _
                       .Location = feed.Element(nsGoogle + "location").Value, _
                       .Photos = feed.Element(nsGoogle + "numphotos").Value, _
                       .Title = feed.Element(nsMrss + "group").Element(nsMrss + "title").Value, _
                       .Description = feed.Element(nsMrss + "group").Element(nsMrss + "description").Value, _
                       .PreviewURL = feed.Element(nsMrss + "group").Element(nsMrss + "thumbnail").Attribute("url").Value _
                    }
        Return feeds.ToList
    End Function

    Private Function LoadPhotos(ByVal str As String) As List(Of Photo)
        Dim feedXML As XDocument = XDocument.Parse(str)
        Dim feeds = From feed In feedXML.Descendants(nsAtom + "entry") _
                   Select New Photo With { _
                      .ID = feed.Element(nsGoogle + "id").Value, _
                      .Caption = feed.Element(nsAtom + "summary").Value, _
                      .Width = feed.Element(nsGoogle + "width").Value, _
                      .Height = feed.Element(nsGoogle + "height").Value, _
                      .Size = feed.Element(nsGoogle + "size").Value, _
                      .ImageURL = feed.Element(nsMrss + "group").Element(nsMrss + "content").Attribute("url").Value, _
                      .PreviewURL = feed.Element(nsMrss + "group").Elements(nsMrss + "thumbnail")(1).Attribute("url").Value _
                   }
        Return feeds.ToList
    End Function

#End Region

End Class